package com.entrogy.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.entrogy.bean.*;
import com.entrogy.dao.MeetingRoomMapper;
import com.entrogy.dao.MessageMapper;
import com.entrogy.dao.UserMapper;
import com.entrogy.service.MeetingRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName MeetingRoomServiceImpl
 * @Description
 * @Author luoxingjiang
 * @Date 2019/8/6 0006 下午 3:45
 * @Version 1.0
 **/
@Service
public class MeetingRoomServiceImpl implements MeetingRoomService {

    @Autowired
    private MeetingRoomMapper meetingRoomMapper;

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<MeetingRoomEntity> listMeetingRoom(String meetingRoomName, Integer schoolCode, String selectDate, Integer meetingRoomId) {
        List<MeetingRoomEntity> meetingRoomList = meetingRoomMapper.listMeetingRoom(meetingRoomName, schoolCode, meetingRoomId);
        for (MeetingRoomEntity meetingRoom : meetingRoomList) {
            Integer roomId = meetingRoom.getId();
            List<JSONObject> disableTime = meetingRoomMapper.listMeetingRoomDisableTime(roomId, selectDate);
            meetingRoom.setDisableTime(disableTime);
        }
        return meetingRoomList;
    }

    @Override
    public boolean insertMeetingApplyRecord(MeetingApplyRecordEntity meetingApplyRecordEntity) {
        //查询管理该会议室的管理员以及申请人的基本信息
        Integer meetingRoomId = meetingApplyRecordEntity.getMeetingRoomId();
        String managerUserId = meetingRoomMapper.getManagerInfo(meetingRoomId);
        UserEntity applyUser = userMapper.getUserByUid(meetingApplyRecordEntity.getApplyUserId());
        meetingApplyRecordEntity.setManagerUserId(managerUserId).setApplyUsername(applyUser.getName());
        //首先插入会议室申请记录表
        Integer insertMeetingApplyRecordRow = meetingRoomMapper.insertMeetingApplyRecord(meetingApplyRecordEntity);
        List<String> meetingPersonIdList = new ArrayList<>();
        List<JSONObject> meetingPersonList = meetingApplyRecordEntity.getMeetingPersonList();
        for (JSONObject person : meetingPersonList) {
            meetingPersonIdList.add(person.getString("id"));
        }
        //申请人自己也要参会
        if (!meetingPersonIdList.contains(meetingApplyRecordEntity.getApplyUserId())){
            meetingPersonIdList.add(meetingApplyRecordEntity.getApplyUserId());
        }
        //然后在会议人员记录表内插入记录
        Integer insertMeetingPersonRow = meetingRoomMapper.insertMeetingPersonRecord(meetingApplyRecordEntity.getId(), meetingPersonIdList);
        return insertMeetingApplyRecordRow != null && insertMeetingApplyRecordRow.equals(1) && insertMeetingPersonRow != null && insertMeetingPersonRow.equals(meetingPersonIdList.size());
    }

    @Override
    public String getMeetingRoomAdminPhone(Integer meetingRoomId) {
        return meetingRoomMapper.getMeetingRoomAdminPhone(meetingRoomId);
    }

    @Override
    public List<MeetingApplyRecordEntity> listMeetingApplyRecord(String userId) {
        return meetingRoomMapper.listMeetingApplyRecord(userId);
    }

    @Override
    public MeetingApplyRecordEntity getApplyDetail(Integer meetingRoomApplyId) {
        return meetingRoomMapper.getApplyDetail(meetingRoomApplyId);
    }

    @Override
    public boolean updateMeetingRoomApplyRecord(Integer meetingRoomApplyId, Integer audited, String opinion) {
        return meetingRoomMapper.updateMeetingRoomApplyRecord(meetingRoomApplyId, audited, opinion);
    }

    @Override
    public PubCountEntity getApplyUserOpenId(Integer meetingRoomApplyId) {
        String phone = meetingRoomMapper.getApplyUserPhone(meetingRoomApplyId);
        return messageMapper.getPubCountOpenidByPhone(phone);
    }

    @Override
    public List<String> listMeetingPersonPhone(Integer meetingRoomApplyId) {
        return meetingRoomMapper.listMeetingPersonPhone(meetingRoomApplyId);
    }

    @Override
    public List<JSONObject> getMeetingRoomListSimple(Integer schoolCode) {
        return meetingRoomMapper.getMeetingRoomListSimple(schoolCode);
    }

    @Override
    public List<MeetingApplyRecordEntity> listAttendanceMeeting(String userId) {
        return meetingRoomMapper.listAttendanceMeeting(userId);
    }

    @Override
    public List<MeetingApplyRecordEntity> listMyApply(String userId) {
        return meetingRoomMapper.listMyApply(userId);
    }

    @Override
    public boolean insertMeetingRoom(MeetingRoomEntity meetingRoom) {
        //查询管理员基本信息
        UserEntity manager = userMapper.getUserByUid(meetingRoom.getManagerUserId());
        meetingRoom.setManagerPhone(manager.getMobile())
                .setManagerUserPhoto(manager.getPhoto())
                .setManagerUsername(manager.getName())
                .setSchoolCode(manager.getSchoolCode());
        Integer row = meetingRoomMapper.insertMeetingRoom(meetingRoom);
        return row != null && row == 1;
    }

    @Override
    public boolean isRequireAudit(Integer meetingRoomId) {
        Integer requireAudit = meetingRoomMapper.getRequireAuditCode(meetingRoomId);
        //requireAudit为0表示不需要审核，为1表示需要审核
        return requireAudit != null && requireAudit.equals(1);
    }

    @Override
    public Integer countMeetingApplyRecord(String userId) {
        return meetingRoomMapper.countMeetingApplyRecord(userId);
    }

    @Override
    public Integer countAttendanceMeeting(String userId) {
        return meetingRoomMapper.countAttendanceMeeting(userId);
    }

    @Override
    public Integer countMyApply(String userId) {
        return meetingRoomMapper.countMyApply(userId);
    }

    @Override
    public JSONObject getMeetingDate(Integer meetingRoomApplyId) {
        return meetingRoomMapper.getMeetingDate(meetingRoomApplyId);
    }

    @Override
    public List<PersonMeetingAttendanceEntity> listAttendancePerson(Integer meetingRoomApplyId, String tableName, String meetingEndTime) {
        //根据会议查询会议室id
        Integer meetingRoomId = meetingRoomMapper.getMeetingRoomId(meetingRoomApplyId);
        //查询会议室的设备
        List<String> deviceIds = meetingRoomMapper.getMeetingRoomDevice(meetingRoomId);
        //查询在该会议室的设备上，在开会截止时间的当天有打卡记录的人员记录
        List<PersonMeetingAttendanceEntity> attendancePersonList = meetingRoomMapper.listAttendancePerson(meetingRoomApplyId, tableName, meetingEndTime, deviceIds.size() == 0 ? null : deviceIds);
        return attendancePersonList;
    }

    @Override
    public List<PersonMeetingAttendanceEntity> listAllMeetingPerson(Integer meetingRoomApplyId) {
        return meetingRoomMapper.listAllMeetingPerson(meetingRoomApplyId);
    }

    @Override
    public boolean updateMeetingRoom(MeetingRoomEntity meetingRoom) {
        // 查询管理员基本信息
        UserEntity manager = userMapper.getUserByUid(meetingRoom.getManagerUserId());
        if (manager == null){

        }
        meetingRoom.setManagerPhone(manager.getMobile())
                .setManagerUserPhoto(manager.getPhoto())
                .setManagerUsername(manager.getName())
                .setSchoolCode(manager.getSchoolCode());
        Integer row = meetingRoomMapper.updateMeetingRoom(meetingRoom);
        return row != null && row == 1;
    }

    @Override
    public List<String> listMeetingRoomDevice(MeetingApplyRecordEntity meetingApplyRecord) {
        return meetingRoomMapper.listMeetingRoomDevice(meetingApplyRecord);
    }

    @Override
    public List<String> listMeetingPersonThdid(Integer meetingId) {
        return meetingRoomMapper.listMeetingPersonThdid(meetingId);
    }

    @Override
    public boolean isMeetingDormManager(String uid) {
        List<Integer> meetingRoomIds = meetingRoomMapper.listMeetingRoomIdByManagerId(uid);
        return meetingRoomIds != null && meetingRoomIds.size() > 0;
    }

    @Override
    public List<Integer> listApplyId(Integer schoolCode, String uid) {
        return meetingRoomMapper.listApplyId(schoolCode, uid);
    }

}
